package src.main.java.org.reflect.obtainMethod;

import java.lang.reflect.Method;

public class TestMethod {
    public static void main(String[] args) throws Exception {
        Class<Cat> catClass = Cat.class;
        Method[] methods = catClass.getMethods();//获取类的全部成员方法，public的
        Method[] declaredMethods = catClass.getDeclaredMethods(); //获取全部的成员方法
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod.getName()+
                    "===="+declaredMethod.getParameterCount()+
                    "==="+declaredMethod.getReturnType());
        }
        System.out.println("=====================");
        //定位单个方法
        Method run = catClass.getDeclaredMethod("run");
        System.out.println(run.getName()+
                "===="+run.getParameterCount()+
                "==="+run.getReturnType());
        Method eat = catClass.getDeclaredMethod("eat", String.class);
        System.out.println(eat.getName()+
                "===="+eat.getParameterCount()+
                "==="+eat.getReturnType());

        Cat c = new Cat();
        run.setAccessible(true);
        eat.setAccessible(true);
        run.invoke(c);
        eat.invoke(c,"饼干");
    }
}
